package xyz.prob.kun.base.security.domain;


import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import xyz.prob.kun.base.security.model.UserAuthModel;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 登录用户身份权限
 * 
 * @author ruoyi
 */
@Data
public class LoginUser implements UserDetails
{
    private static final long serialVersionUID = 1L;

    /**
     * 用户ID
     */
    private long userId;


    /**
     * 过期时间,时间戳，秒
     */
    private long expireTime;


    /**
     * 权限列表
     */
    private Set<String> permissions;

    private Set<String> roles;

    private String password;

    private String username;

    private boolean enable;





    @Override
    public String getPassword()
    {
        return password;
    }

    @Override
    public String getUsername()
    {
        return username;
    }

    /**
     * 账户是否未过期,过期无法验证
     */

    @Override
    public boolean isAccountNonExpired()
    {

        return expireTime>System.currentTimeMillis()/1000;
    }

    /**
     * 指定用户是否解锁,锁定的用户无法进行身份验证
     * 
     * @return
     */

    @Override
    public boolean isAccountNonLocked()
    {
        return true;
    }

    /**
     * 指示是否已过期的用户的凭据(密码),过期的凭据防止认证
     * 
     * @return
     */

    @Override
    public boolean isCredentialsNonExpired()
    {
        return true;
    }

    /**
     * 是否可用 ,禁用的用户不能身份验证
     * 
     * @return
     */

    @Override
    public boolean isEnabled()
    {
        return enable;
    }


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities()
    {
        return null;
    }

    public static LoginUser createOf(UserAuthModel userAuthModel){
        LoginUser loginUser = new LoginUser();
        loginUser.userId = userAuthModel.getId();
        loginUser.username = userAuthModel.getUsername();
        loginUser.password = userAuthModel.getPassword();
        loginUser.enable = userAuthModel.isEnable();
        return loginUser;
    }
}
